package com.liucxu.jiandanl.View.Adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gc.materialdesign.widgets.Dialog;
import com.liucxu.jiandanl.Entity.Post;
import com.liucxu.jiandanl.R;
import com.liucxu.jiandanl.Utils.ViewHolder;
import com.liucxu.jiandanl.View.Activity.FreshNewsDetailActivity;
import com.liucxu.jiandanl.View.Fragment.FreshNewsFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 无聊图的Adapter
 * Created by liuchenxu on 2016/8/4.
 */
public class FreshNewsAdapter extends BaseAdapter {

    private List<Post> items;

    private FreshNewsFragment mContext;

    public FreshNewsAdapter(FreshNewsFragment context) {
        items = new ArrayList<Post>();
        mContext = context;
    }

    public void setDatas(List<Post> datas) {
        if (datas != null) {
            items.clear();
            items.addAll(datas);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = View.inflate(mContext.getActivity(), R.layout.adapter_fresh_news, null);
        }
        initView(view,i);
        return view;
    }

    /**
     *
     * @param view
     * @param position
     */
    private void initView(View view,int position) {
        final Post post=items.get(position);
        ImageView img= ViewHolder.get(view,R.id.adapter_img);
        TextView artical_title=ViewHolder.get(view,R.id.artical_title);
        TextView adapter_author=ViewHolder.get(view,R.id.adapter_author);
        TextView adapter_chanel=ViewHolder.get(view,R.id.adapter_chanel);
        TextView adapter_brow_num=ViewHolder.get(view,R.id.adapter_brow_num);
        TextView adapter_share=ViewHolder.get(view,R.id.adapter_share);
        RelativeLayout adapter_root=ViewHolder.get(view,R.id.adapter_root);
        adapter_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext.getActivity(), FreshNewsDetailActivity.class);
                intent.putExtra("POST",post);
                mContext.getActivity().startActivity(intent);
            }
        });
//        GlideUtils.getInstance().LoadSupportv4FragmentBitmap(mContext,post.getCustom_fields().getThumb_c().get(0),img,R.drawable.ic_loading_large,R.drawable.ic_loading_large,GlideUtils.LOAD_BITMAP);
//        ImageLoaderUtils.displayImageWithLoadingPicture(post.getCustom_fields().getThumb_c().get(0).replace("custom", "medium"),img);
        artical_title.setText(post.getTitle());
        adapter_author.setText(post.getAuthor().getNickname());
        adapter_chanel.setText("@"+post.getTags().get(0).getTitle());
        adapter_brow_num.setText("浏览"+post.getTags().get(0).getPost_count()+"次");
        adapter_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(mContext.getActivity(),"分享", "确认分享?");
                dialog.setOnAcceptButtonClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialog.setOnCancelButtonClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
            }
        });
    }
}
